﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JsApiPayPage.aspx.cs" Inherits="WxPayAPI.JsApiPayPage" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>微信安全支付</title>
    <link href="../../Styles/jquery.validate.css" rel="stylesheet" type="text/css" />
    <link href="../../Styles/jquery.mobile-1.4.5.min.css" rel="stylesheet" type="text/css" />
    <link href="../../Styles/jquery.mobile.custom.css" rel="stylesheet" type="text/css" />
    <link href="../../weui-master/dist/style/weui.css" rel="stylesheet" type="text/css" />
    <link href="../../weui-master/dist/example/example.css" rel="stylesheet" type="text/css" />

    <script src="../../Scripts/jquery.min.js" type="text/javascript"></script>

    <script src="../../Scripts/jquery.mobile-1.4.5.min.js" type="text/javascript"></script>

    <script src="../../Scripts/jquery.cookie-validate.min.js" type="text/javascript"></script>

</head>

<script type="text/javascript">

               //调用微信JS api 支付
               function jsApiCall()
               {
               
                  WeixinJSBridge.invoke('getBrandWCPayRequest',<%=wxJsApiParam%>,function(res){
                  //alert(res.err_msg+res.err_desc);
                  //跳转到支付结果页，结果页再去微信查询订单，获得支付结果状态，然后再做后续处理。                  
                  $.ajax({
                        type: 'GET', // 'POST',                     
                        url: "OrderQueryPage.aspx?out_trade_no=<%=out_trade_no%>",
                        data: {},
                        success: function(e) {                          
                          if(e.trade_state=="SUCCESS")
                          {
                              //alert("1:支付成功");  
                          }
                          else
                          {
                              if(e.trade_state_desc!=null && e.trade_state_desc!="")
                              {
                                 //alert("2:"+e.trade_state_desc); 
                              } 
                              else
                              { 
                                 trade_state_desc="支付失败";
                                 //alert("3:支付失败");
                              }                          
                          }
                          var dt = new Date().getTime();//res.err_msg+res.err_desc
                          var paramter="dt="+dt+"&from=redirect&status="+e.trade_state+"&orderCode=<%=out_trade_no%>"+"&msg="+e.trade_state_desc+"&err_msg="+res.err_msg+"&err_desc="+res.err_desc;
                          var url_1="<%=sucessurl%>?"+paramter; 
                          //alert("4:"+url_1);
                          //alert(url_1);
                          window.location.href=url_1;                      
                        },
                        complete: function(XMLHttpRequest, textStatus) {
                             //alert("complete:"+textStatus);  
                        },
                        dataType: 'json',
                        async: false
                    });

                   });  
               }
               
                
               function callpay()
               { 
                   $("#Button1").attr("disabled","disabled");
                   $("#Button1").css({"background-color":"#D4D4D4"}); 
                   if (typeof(WeixinJSBridge) == "undefined")
                   {
                       if (document.addEventListener)
                       {
                           document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
                       }
                       else if (document.attachEvent)
                       {
                           document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                           document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
                       }
                   }
                   else
                   {
                       jsApiCall();
                   }
               }
               
               callpay();
               
</script>

<body>
    <form>
    <%if (!string.IsNullOrEmpty(msg))
      {%>
    <div class="weui_msg">
        <div class="weui_opr_area">
            <span class="weui_icon_warn"></span>
            <%=msg%>
        </div>
    </div>
    <%} %>
    </form>
</body>
</html>
